<div class="layui-fluid">
	<div class="layui-row">
		<div class="layui-col-xs12">

			<table id="teachertable" lay-filter="type_hash" lay-even lay-skin="nob"></table>
			<script type="text/html" id="barteachertable">
				<a class="layui-btn" lay-event="look">主页</a>
				<a class="layui-btn" lay-event="edit">编辑</a>
								<!-- <a class="layui-btn" lay-event="look">数据</a> -->
								<a class="layui-btn" lay-event="del">删除</a>
       </script>

		</div>
	</div>
</div>
<script type="text/html" id="teachertoolbar">
	<div class="toobarbox">
	<div class="layui-btn-container" >
    <button class="layui-btn " lay-event="addbtn">添加讲师</button>
   <!-- <button class="layui-btn layui-btn-primary" lay-event="delallbtn">删除选中</button> -->
  </div>
		<div class="layui-inline">
			<input type="text" class="layui-input" id="dataxx" placeholder="选择日期">
		</div>
		<div class="layui-inline">
			<select name="tbType" lay-filter="shelvesfilter" id="tbtypeid">
				<option value=" ">全部</option>
				<option value="1">启用</option>
					<option value="0">禁用</option>
			</select>
		</div>
		<div class="iconfont iconsousuo tablesearch">
			<input type="text" class="layui-input " id="tablesearchid" title="请输入搜索的关键字" placeholder="请输入搜索关键字">
	
		</div>
			</div>
</script>


<script type="text/html" id="headimgTp">
	<!-- 这里的 checked 的状态只是演示 -->
 <img src="{{ IMG_URL + d.photo }}"  style="height: 100%;"  alt="讲师头像"  >
</script>

<script type="text/html" id="StatusTp">
	<!-- 这里的 checked 的状态只是演示 -->

<input type="checkbox" name="status"  value="{{d.tbId}}" lay-text="启用|禁用" lay-skin="switch"  lay-filter="lockDemo" {{ d.status == 1 ? 'checked' : '' }} >
</script>
<script>
	var xxjson = {
				search:'',
				startTime:'',
				stopTime:'',
				tbType:''
	}
	layui.config({
		base: '/src/js/'
	}).use(['jquery', 'mockjs', 'table', 'sidebar', 'form', 'laydate'], function() {
		var $ = layui.jquery,
			layer = layui.layer,
			table = layui.table,
			sidebar = layui.sidebar,
			form = layui.form , laydate = layui.laydate;
		var tablesearchidval;
		//第一个实例
		var typetable = table.render({
			size: 'lg',
			where: xxjson,
			id: 'teachertableid',
			contentType: 'application/json',
			pageSize: 20,
			elem: '#teachertable',
			toolbar: '#teachertoolbar',
					height: 'full-100',
			url: HH_URL + LecturerGet, //数据接口
			page: true, //开启分页
			even: true,
			skin: 'nob',
			cellMinWidth: 100,
			cols: [
				[ //表头
					{
						type: 'numbers',
						// fixed: 'left',
						title: '序号',
						width: 50
					},
				 {
						field: 'tbId',
						title: 'ID',
						hide: true
					}, {
						field: 'tbHeadPortrait',
						title: '讲师头像',
						templet: '#headimgTp',
						event: 'lookheadimg',
						width: 100
					}, {
						field: 'realName',
						title: '真实姓名',
						width: 100
					}, {
						field: 'tbUserID',
						title: '讲师ID',
						width: 200
					}, {
						field: 'bq',
						title: '类型',
						templet: function(d) {
							try {
								var produstsTypes = ''
								if (d.produstsTypes.length > 0) {
									$.each(d.produstsTypes, function(idx, obj) {
										if (idx > 0) {
											produstsTypes += ' / '
										}
										produstsTypes += obj.tbProdustsTypeName;
									})
									return produstsTypes;
								} else {
									return '无'
								}


							} catch (e) {
								return '无'
								//TODO handle the exception
							}
						},
					}, 	 {
						field: 'tbSourceName',
						title: '来源渠道',
						width: 200
					},{
						field: 'createTime',
						title: '创建时间',
						width: 200
					},	{
						type: 'status',
						title: '状态',
						width: 100,
						templet: '#StatusTp',
						unresize: true
					},  {
						title: '操作',
						fixed: 'right',
						width: 180,
						toolbar: '#barteachertable'
					}
				]
			],

			done: function(res, curr, cont) {
					laydate.render({
						elem: '#dataxx', //指定元素
						range: true,
						done: function(value, date, endDate) {
							if (date.year == undefined) {
								xxjson.startTime = '';
								xxjson.stopTime = '';
							} else {
								xxjson.startTime = date.year + '-' + date.month + '-' + date.date;
								xxjson.stopTime = endDate.year + '-' + endDate.month + '-' + endDate.date;
							}
							typetable.reload({
								where: xxjson,
								page: {
									curr: 1 //重新从第 1 页开始
								}
							});
							cshfn()
						}
					});
					
					form.on('select(shelvesfilter)', function(data) {
						xxjson.tbType = data.value
						typetable.reload({
							where: xxjson,
							page: {
								curr: 1 //重新从第 1 页开始
							}
						});
						cshfn()
					});
					
					
					
				$('#tablesearchid').keydown(function(event) {
					if (event.keyCode == 13) {
						tablesearchidval = $('#tablesearchid').val();
						xxjson.search = tablesearchidval;
						typetable.reload({
							where: xxjson,
							page: {
								curr: 1 //重新从第 1 页开始
							}
						});
						cshfn()
					}
				})
			}
		});

		//监听表格开关
		form.on('switch(lockDemo)', function(obj) {
	
			// var index  = obj.othis.parents('tr').;
					console.log(obj.value)
					var tbType = obj.elem.checked ? 1 : 0;
			POSTAJAX(LecturerMod,{
				tbId:obj.value,
				tbType:tbType
			},function(res){
				layer.msg(res.msg)
			})


		});
		
		function cshfn() {
			$("#tbtypeid").val(xxjson.tbType)
			$('#tablesearchid').val(tablesearchidval);
			if (xxjson.startTime == null || xxjson.startTime == '') {
				$('#dataxx').val('');
			} else {
				$('#dataxx').val(xxjson.startTime + ' - ' + xxjson.stopTime);
			}
		
			// $('#dataxx').val(xxjson.startTime + ' - ' + xxjson.stopTime)
		}

		//监听事件
		table.on('toolbar(type_hash)', function(obj) {
			var checkStatus = table.checkStatus(obj.config.id);
			var data = obj.data; //获得当前行数据
			var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
			var tr = obj.tr; //获得当前行 tr 的DOM对象
			switch (layEvent) {
				case 'delallbtn':
					//批量删除行数
					// console.log(checkStatus.data);
					var tbarray = [];
					$.each(checkStatus.data, function(idx, obj) {
						tbarray.push(obj.tbId);
					})

					layer.confirm('确认删除所选中的讲师吗11?', function(index) {
						// POSTAJAX(LecturerDel, {
						// 	tbId:
						// 	ids: tbarray
						// }, function(res) {
						// 	layer.close(index);
						// 	layer.msg(res.msg);
						// 	if (res.status == 200) {
						// 		typetable.reload('teachertable');
						// 	}
						// })
						//向服务端发送删除指令
					});

					break;
				case 'addbtn':
					location.href = '#/teacher/add'
					break;
				default:

					break;
			}
		});



		//监听工具条
		table.on('tool(type_hash)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
			var data = obj.data; //获得当前行数据
			var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
			var tr = obj.tr; //获得当前行 tr 的DOM对象
			switch (layEvent) {
				case 'look':
					location.href = '#/teacher/homepage?tbId=' + obj.data.tbId
					// layer.msg(JSON.stringify(obj.data));
					break;
				case 'del':
					delfn({
						tbId: obj.data.tbId

					}, obj);

					break;
				case 'edit':
					location.href = '#/teacher/add?tbId=' + obj.data.tbId
					break;
				case 'lookheadimg':
					layer.open({
						type: 1,
						title: false,
						closeBtn: 0,
						area: '200px',
						skin: 'layui-layer-nobg', //没有背景色
						shadeClose: true,
						content: ' <img src="' + IMG_URL + obj.data.photo + '"  alt="讲师头像"  >'
					});
					// layer.msg("图图图")
					break;
				default:
					break;
			}
		});

		function delfn(tdata, obj) {
			layer.confirm('确认删除该讲师吗?', function(index) {
				POSTAJAX(LecturerDel, tdata, function(res) {
					layer.close(index);
					layer.msg(res.msg);
					if (res.status == 200) {
						obj.del(); //删除对应行（tr）的DOM结构，并更新缓存

					}

				})

				//向服务端发送删除指令
			});
		}



	});
</script>

<style scoped>

</style>
